/*
 * @(#)Main.java  
 * 
 * Copyright (c) 2011 Werner Randelshofer, Goldau, Switzerland.
 * All rights reserved.
 * 
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */
package org.monte.jmftsccdemo;

import java.awt.BorderLayout;
import java.io.File;
import javax.media.Controller;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Manager;
import javax.media.Player;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

/**
 * Main.
 *
 * @author Werner Randelshofer
 * @version $Id: Main.java 304 2013-01-03 07:45:40Z werner $
 */
public class Main extends javax.swing.JFrame {

    private JFileChooser generateChooser;
    private JFileChooser openChooser;
    private Player player;

    /** Creates new form Main */
    public Main() {
        initComponents();
        setSize(400, 400);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        infoLabel = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        generateVideoItem = new javax.swing.JMenuItem();
        openVideoItem = new javax.swing.JMenuItem();

        FormListener formListener = new FormListener();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("TSCC Codec for JMF - Demo");

        infoLabel.setText("<html><b>This is a demo for the TSCCCodec class.</b><br>\nThis class supports encoding and decoding of the Techsmith Screen Capture format with the Java Media Framework ( JMF).<br>\nCopyright © 2011 Werner Randelshofer.<br>\nThis software can be licensed under Creative Commons Attribution 3.0<br>");
        infoLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder(8, 12, 12, 12));
        getContentPane().add(infoLabel, java.awt.BorderLayout.PAGE_START);

        fileMenu.setText("File");

        generateVideoItem.setText("Generate Sample Movies...");
        generateVideoItem.addActionListener(formListener);
        fileMenu.add(generateVideoItem);

        openVideoItem.setText("Open Movie...");
        openVideoItem.addActionListener(formListener);
        fileMenu.add(openVideoItem);

        menuBar.add(fileMenu);

        setJMenuBar(menuBar);

        pack();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == generateVideoItem) {
                Main.this.generatePerformed(evt);
            }
            else if (evt.getSource() == openVideoItem) {
                Main.this.openPerformed(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

private void generatePerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generatePerformed
    if (generateChooser == null) {
        generateChooser = new JFileChooser();
        generateChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    }
    if (JFileChooser.APPROVE_OPTION == generateChooser.showDialog(this, "Choose Output Folder")) {
        final File f = generateChooser.getSelectedFile();
        final JProgressBar pb = new JProgressBar();
        pb.setIndeterminate(true);
        getContentPane().add(pb, BorderLayout.SOUTH);
        ((JComponent) getContentPane()).revalidate();
        SwingWorker w = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {
                    org.monte.jmfavidemo.Main.main(new String[]{f.getPath()});
                return null;
            }

            @Override
            protected void done() {
                pb.setIndeterminate(false);
                getContentPane().remove(pb);
                ((JComponent) getContentPane()).revalidate();
                getContentPane().repaint();
            }
            
        };
        new Thread(w).start();
    }
}//GEN-LAST:event_generatePerformed

private void openPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openPerformed
    if (openChooser == null) {
        openChooser = new JFileChooser();
    }
    if (JFileChooser.APPROVE_OPTION == openChooser.showOpenDialog(this)) {
        final File f = openChooser.getSelectedFile();
        try {
            final Player newPlayer = Manager.createPlayer(Manager.createDataSource(f.toURI().toURL()));
            newPlayer.addControllerListener(new ControllerListener() {

                @Override
                public void controllerUpdate(ControllerEvent event) {
                    if (newPlayer.getState() == Controller.Realized) {
                        newPlayer.removeControllerListener(this);

                        getContentPane().removeAll();
                        JPanel panel = new JPanel(new BorderLayout());
                        panel.add(newPlayer.getVisualComponent(), BorderLayout.CENTER);
                        panel.add(newPlayer.getControlPanelComponent(), BorderLayout.SOUTH);
                        add(panel, BorderLayout.CENTER);
                        pack();

                        if (player != null) {
                            player.close();
                        }
                        player = newPlayer;
                        getRootPane().putClientProperty("Window.documentFile", f);
                        setTitle(f.getName());
                    }
                }
            });
            newPlayer.realize();
        } catch (Exception ex) {
            JLabel label = new JLabel(ex.toString());
            add(label, BorderLayout.CENTER);
            ((JComponent) getContentPane()).revalidate();
            getContentPane().repaint();
            ex.printStackTrace();
        }
    }

}//GEN-LAST:event_openPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        
        Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
        
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenuItem generateVideoItem;
    private javax.swing.JLabel infoLabel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem openVideoItem;
    // End of variables declaration//GEN-END:variables
}
